import { markRaw, type RendererElement, type RendererNode, type VNode } from 'vue'
import { ElMessageBox } from 'element-plus'
import { SuccessFilled, WarningFilled, CircleCloseFilled, QuestionFilled, WarnTriangleFilled } from '@element-plus/icons-vue'
// import type { Action } from 'element-plus'
let alertFlag = false;//记录多少alert弹窗，只展示一个


const MessageBox = {
  /** 消息提示-失败 */
  alert: (data: { type?: string; txt: string; title?: string; confirmButtonText?: string; }) => {
    const type = data.type || 'success';
    const txt = data.txt || '消息提示';
    const title = data.title || '';
    const confirmButtonText = data.confirmButtonText || '我知道了';
    let icon = SuccessFilled;
    type == 'warning' ? icon = WarningFilled : '';
    type == 'danger' ? icon = CircleCloseFilled : '';
    let iconClass = 'alert-icon-class-success';
    type == 'warning' ? iconClass = 'alert-icon-class-warning' : '';
    type == 'danger' ? iconClass = 'alert-icon-class-danger' : '';
    return new Promise((resolve, reject) => {
      if (!alertFlag) {
        alertFlag = true;
        ElMessageBox.alert(txt, title, {
          customClass: 'messageBox-alert-customClass ' + iconClass,
          icon: markRaw(icon),
          confirmButtonText: confirmButtonText,
          confirmButtonClass: 'alert-confirm-button',
          center: true,
          showClose: false,
          callback: (action: any) => {
            resolve(action);
            alertFlag = false;
          },
        });
      } else {
        reject();
      }
    });
  },
  
  /** 确认消息 */
  enter: (data: { txt: any; type: any; confirmButtonText: string; cancelButtonText: string; closeOnClickModal: undefined; closeOnPressEscape: undefined; }) => {
    const txt = data.txt || '确认提交吗？';
    const type = data.type || 'warning';
    const confirmButtonText = data.confirmButtonText || '确定';
    const cancelButtonText = data.cancelButtonText || '取消';
    const closeOnClickModal = data.closeOnClickModal != undefined ? data.closeOnClickModal : false;//是否通过点击遮罩层关闭提示框
    const closeOnPressEscape = data.closeOnPressEscape != undefined ? data.closeOnPressEscape : false;//是否通过点击ESC关闭提示框
    return new Promise((resolve, reject) => {
      ElMessageBox.confirm(
        txt,
        type,
        {
          customClass: 'messageBox-alert-customClass ',
          confirmButtonText: confirmButtonText,
          cancelButtonText: cancelButtonText,
          cancelButtonClass: 'enter-cancel-button-calss',
          closeOnClickModal: closeOnClickModal,
          closeOnPressEscape: closeOnPressEscape,
          type: type,
          center: true,
          title: '',
          showClose: false,
        }
      ).then((res) => {
        resolve(res);
      }).catch((err) => {
        reject(err);
      });
    });
  },
  /** 提交内容 */
  prompt: (data: { txt: string; title: string; confirmButtonText: string; cancelButtonText: string; inputPattern: any; inputErrorMessage: string; closeOnClickModal: undefined; closeOnPressEscape: undefined; autofocus: undefined; }) => {
    const txt = data.txt || '确认提交吗？';
    const title = data.title || '提示';
    const confirmButtonText = data.confirmButtonText || '确定';
    const cancelButtonText = data.cancelButtonText || '取消';
    const inputPattern = data.inputPattern || '';
    const inputErrorMessage = data.inputErrorMessage || '信息格式错误！'
    const closeOnClickModal = data.closeOnClickModal != undefined ? data.closeOnClickModal : false;//是否通过点击遮罩层关闭提示框
    const closeOnPressEscape = data.closeOnPressEscape != undefined ? data.closeOnPressEscape : false;//是否通过点击ESC关闭提示框
    const autofocus = data.autofocus != undefined ? data.autofocus : true;//是否自动获取焦点
    return new Promise((resolve, reject) => {
      ElMessageBox.prompt(txt, title, {
        customClass: 'messageBox-alert-customClass messageBox-alert-customClass-prompt',
        confirmButtonText: confirmButtonText,
        cancelButtonText: cancelButtonText,
        cancelButtonClass: 'enter-cancel-button-calss',
        inputPattern: inputPattern,
        inputErrorMessage: inputErrorMessage,
        closeOnClickModal: closeOnClickModal,
        closeOnPressEscape: closeOnPressEscape,
        autofocus: autofocus,
        showClose: false,
      })
        .then(({ value }) => {
          resolve(value)
        })
        .catch((err) => {
          reject(err)
        })
    })
  }

}
export default MessageBox
